package com.sm.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.sm.model.*;
import com.sm.service.*;
import com.sm.util.Msg;
import com.sm.util.PageResult;
import com.sm.vo.UserHappyCommentVo;
import com.sm.vo.UserRequestVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
@RequestMapping("/happyRequest")
public class HappyRequestController {

    @Autowired
    private HappyRequestService happyRequestService;
    @Autowired
    private HappySolveService happySolveService;
    @Autowired
    private SolveCommentService solveCommentService;
    @Autowired
    private UserService userService;
    @Autowired
    private CollectService collectService;
    @Autowired
    private LikeHappyRequestService likeHappyRequestService;
    /**
     *  幸福问答提问接口(不带支付功能)
     * @param
     * @param title
     * @param content
     * @return
     */
    @PostMapping("/insertRequest")
    @ResponseBody
    public Msg insertRequest(Integer userId,String title,String content) {
        HappyRequest happyRequest = new HappyRequest();
        if (StrUtil.isBlank(title) || StrUtil.isBlank(content)) {
            return Msg.success().add("msg", "请填写内容");
        }
        if (ObjectUtil.isEmpty(userId)||userId == null){
            return Msg.fail().add("msg","没找到用户id");
        }
        happyRequest.setContent(content);
        happyRequest.setTitle(title);
        happyRequest.setCreateTime(new Date());
        happyRequest.setRequestUser(userId);
        int i = happyRequestService.insertRequest(happyRequest);
        if (i == 1) {
            return Msg.success().add("msg", "幸福问答的问题提交成功");
        }
        return Msg.fail().add("msg","幸福问答的问题提交失败");
    }


    /**
     *  幸福问答--全部问题列表
     */
    @ResponseBody
    @RequestMapping("/selectHapptyRequestList")
    public Msg selectHapptyRequestList(Integer currentPage,Integer pageSize,Integer userId){
        PageResult pageResult = happyRequestService.selectHapptyRequestList(currentPage, pageSize);
        if (ObjectUtil.isEmpty(pageResult)){
            return Msg.success().add("msg","暂无数据");
        }
        List<HappyRequest> rows = pageResult.getRows();
        List<UserRequestVo> vos = new ArrayList<>();
        for (HappyRequest r : rows) {
            UserRequestVo userRequestVo = new UserRequestVo();
            userRequestVo.setRequest(r);
            userRequestVo.setIsCollect(checkIsCollect(userId,r.getRequestId()));
            userRequestVo.setIsLike(checkIsLike(userId,r.getRequestId(),0));
            userRequestVo.setUser(userService.selectAvatarAndNickName(r.getRequestUser()));
            vos.add(userRequestVo);
        }
        return Msg.success(new PageResult(Long.valueOf(vos.size()),vos));
    }

    /**
     * 幸福问答--我的提问列表
     */
    @RequestMapping("/myRequest")
    @ResponseBody
    public Msg myRequest(Integer userId,Integer currentPage,Integer pageSize){
        PageResult pageResult = happyRequestService.myRequest(currentPage, pageSize, userId);
        if (ObjectUtil.isEmpty(pageResult)){
            return Msg.success().add("msg","暂无数据");
        }
        List<UserRequestVo> vos = new ArrayList<>();
        List<HappyRequest> rows = pageResult.getRows();
        for (HappyRequest r : rows) {
            UserRequestVo userRequestVo = new UserRequestVo();
            userRequestVo.setRequest(r);
            userRequestVo.setIsCollect(checkIsCollect(userId,r.getRequestId()));
            userRequestVo.setIsLike(checkIsLike(userId,r.getRequestId(),0));
            userRequestVo.setUser(userService.selectAvatarAndNickName(r.getRequestUser()));
            vos.add(userRequestVo);
        }
        return Msg.success(new PageResult(Long.valueOf(vos.size()),vos));
    }
    //根据userId 和 问题id查询是否点赞
    public Integer checkIsLike(Integer userId,Integer requestId,Integer likeType){
        LikeHappyRequest likeHappyRequest = likeHappyRequestService.selectIsExist(userId, requestId, likeType);
        if (ObjectUtil.isEmpty(likeHappyRequest)){
            return 0;
        }
        return 1;
    }

    //根据userId 和 问题id查询是否收藏
    public Integer checkIsCollect(Integer userId,Integer requestId){
        Collect collect = collectService.selectCollectBy(userId, 2, requestId);
        if (ObjectUtil.isEmpty(collect)){
            return 0;
        }
        return 1;
    }




    /**
     *  幸福问答详情页--问题部分
     */
    @ResponseBody
    @RequestMapping("/selectOne")
    public Msg selectRequestInfo(@RequestParam("requestId")Integer requestId,@RequestParam("userId")Integer userId){
        HappyRequest happyRequest = happyRequestService.selectRequestById(requestId);
        if (ObjectUtil.isEmpty(happyRequest)){
            return Msg.success().add("msg","暂无数据");
        }
        User user = userService.selectAvatarAndNickName(happyRequest.getRequestUser());
        User user1 = userService.selectUserById(userId);
        user.setRole(user1.getRole());
        //查询用户是否收藏此问题
        Integer isCollect = checkIsCollect(userId,requestId);
        //查询用户是否点赞此问题
        Integer isLike = checkIsLike(userId,requestId,0);
        return Msg.success().add("happyRequest",happyRequest).add("isCollect",isCollect).add("isLike",isLike).add("user",user);
    }

    /**
     * 幸福问答详情页--评论部分
     */
    @ResponseBody
    @RequestMapping("/selectSolve")
    public Msg selectSolveComment(Integer requestId,Integer userId){
        if (requestId == null){
            return Msg.fail().add("msg","参数异常");
        }
        HappySolve happySolve = happySolveService.selectSolveByReqeustId(requestId);
        if (ObjectUtil.isEmpty(happySolve)){
            return Msg.fail().add("msg","暂无数据");
        }
        Integer isLike = checkIsLike(userId,happySolve.getSolveId(),1);
        User user = userService.selectAvatarAndNickName(happySolve.getSolveUser());
        return Msg.success().add("happySolve",happySolve).add("user",user).add("isLike",isLike);
    }

    /**
     * 幸福问答详情页--回复部分
     */
    @ResponseBody
    @RequestMapping("/selectComment")
    public Msg selectComment(Integer requestId,Integer currentPage,Integer pageSize){
        List<SolveComment> solveComments = solveCommentService.selectSolveComment(requestId, currentPage, pageSize);
        if (ObjectUtil.isEmpty(solveComments)){
            return Msg.success().add("msg","暂无数据");
        }
        ArrayList<UserHappyCommentVo> vos = new ArrayList<>();
        for (SolveComment s:solveComments){
            UserHappyCommentVo userHappyCommentVo = new UserHappyCommentVo();
            userHappyCommentVo.setComment(s);
            userHappyCommentVo.setUser(userService.selectAvatarAndNickName(s.getCommentUser()));
            if (s.getCommentType() == 2){
                SolveComment solveComment = solveCommentService.selectOne(s.getFatherCommentId());
                userHappyCommentVo.setBeUser(userService.selectAvatarAndNickName(solveComment.getCommentUser()));
            }
            vos.add(userHappyCommentVo);
        }
        return Msg.success(vos);
    }

    /**
     * 分享幸福问答
     */
    @RequestMapping("/share")
    @ResponseBody
    public Msg share(Integer id){
        HappyRequest happyRequest = happyRequestService.selectRequestById(id);
        happyRequest.setShareNum(happyRequest.getShareNum()+1);
        Integer update = happyRequestService.update(happyRequest);
        if (update == 1){
            return Msg.success();
        }
        return Msg.fail();
    }

    /**
     * 幸福问答---回答问题
     */
    @RequestMapping("/addSolve")
    @ResponseBody
    public Msg addSolve(Integer requestId,Integer userId,String content){
        if (requestId == null||userId == null|| StrUtil.isEmpty(content)){
            return Msg.fail().add("msg","参数为空");
        }
        User user = userService.selectUserById(userId);
//        if (user.getRole() != 1){
//            return Msg.fail().add("msg","没有评论权限");
//        }
        HappySolve happySolve = new HappySolve();
        happySolve.setSolveUser(userId);
        happySolve.setCreateTime(new Date());
        happySolve.setContent(content);
        happySolve.setRequestId(requestId);
        int i = happySolveService.insertSolve(happySolve);
        if (i == 1){
            updateCommentNum(requestId);
            return Msg.success();
        }
        return Msg.fail();
    }

    /**
     * 幸福问答---对评论的回复
     * type 1表示对评论的回复，2代表对回复的回复
     */
    @RequestMapping("/addComment")
    @ResponseBody
    public Msg addComment(Integer userId, Integer solveId, Integer requestId, String content, Integer type, @RequestParam(value = "fatherId", required = false) Integer fatherId) {
        if (StrUtil.isBlank(content) || "null".equals(content)) {

            return Msg.fail();
        }
        SolveComment solveComment = new SolveComment();
        solveComment.setContent(content);
        solveComment.setCreateTime(new Date());
        solveComment.setCommentType(type);
        solveComment.setCommentUser(userId);
        solveComment.setRequestId(requestId);
        solveComment.setSolveId(solveId);
        if (type == 2) {
            solveComment.setFatherCommentId(fatherId);
        }
        int i = solveCommentService.insertSolveComment(solveComment);
        if (i == 1) {
            //更改幸福问答的回复次数
            updateCommentNum(requestId);
            //更改这条回答的回复次数
            changeCommentCommentNum(solveId);
            return Msg.success();
        }
        return Msg.fail();
    }

    //将幸福问答的问题评论次数+1
    public void updateCommentNum(Integer requestId){
        HappyRequest happyRequest = happyRequestService.selectRequestById(requestId);
        HappyRequest request = new HappyRequest();
        request.setCommentNum(happyRequest.getCommentNum()+1);
        request.setRequestId(requestId);
        happyRequestService.update(request);
    }

    //将幸福问答评论的评论次数+1
    public void changeCommentCommentNum(Integer solveId){
        HappySolve happySolve = happySolveService.selectOne(solveId);
        HappySolve solve = new HappySolve();
        solve.setSolveId(happySolve.getSolveId());
        solve.setCommentNum(happySolve.getCommentNum()+1);
        happySolveService.updateLikeNum(solve);
    }



}
